angular.module('userApp', ['Ys','Common'])
	.controller('combineCtrl', ['$scope', 'CFG', 'API', 'School',
		function($scope, CFG, API, School) {
			$scope.tips = '请输入两个用户ID';
			$scope.items = {
				phone: '手机号码',
				wxOpenID: '微信openID',
				phoneID: '手机标识码',
				balance: '可用余额',
				rechargeTotal: '累计充值',
				buyTotal: '累计消费',
				presrent: '累计赠送',
				school: '学校',
			};
			$scope.mainUser = {};
			$scope.delUser = {};
			$scope.comUser = {};
			// 能否合并
			$scope.canBeCombine = false;
			function checkCanBeCombine(){
				$scope.canBeCombine = (function(){
					var users = [$scope.mainUser,$scope.delUser];
					for (var i=0; i < 2; i++) {
						var user=users[i];
						if (user.id <= 0 || !user._loaded || user.isDel==1) {
							return false;
						}
					}
					if ($scope.mainUser.id == $scope.delUser.id) {
						return false;
					}
					return true;
				})();
				return $scope.canBeCombine;
			}
			$scope.fetchUser = function(userKey) {
				var user = $scope[userKey];
				user._loaded = false;
				if (!user.id || user.id <= 0) {
					alert('请输入用户ID');
					return false;
				}
				if ($scope.mainUser.id == $scope.delUser.id) {
					alert('两个账户的ID不能相同');
					$scope[userKey] = {};
					$scope.comUser = {};
					return false;
				}
				var params = {
					uid: user.id,
					format: 'json',
				};
				var url = CFG.adminDir + 'user/detail';
				API.fetch(url, params).then(function(rtn) {
					if( rtn.user.isDel == 1 ){
						alert('该用户已被删除');

						return false;
					}
					if (rtn.user.phone == '0') {
						rtn.user.phone = '';
					}

					$scope[userKey] = rtn.user;
					$scope[userKey]._loaded = true;
					$scope[userKey].school = School.getSchoolNameById(rtn.user.schoolID);
					// 计算合并后的用户
					if ( ! checkCanBeCombine() ) {
						return ;
					}
					$scope.comUser.id = $scope.mainUser.id;
					['balance', 'rechargeTotal', 'presrent', 'buyTotal'].forEach(function(field) {
						$scope.comUser[field] = parseFloat($scope.mainUser[field] || 0) + parseFloat($scope.delUser[field] || 0);
					});
					['phone', 'nickname', 'phoneID', 'wxOpenID', 'headImgUrl', 'school'].forEach(function(field) {
						if (!$scope.mainUser[field] || $scope.mainUser[field] === '0') {
							$scope.comUser[field] = $scope.delUser[field];
						} else {
							$scope.comUser[field] = $scope.mainUser[field];
						}
					});
				}).catch(function() {
					$scope[userKey] = {};
					$scope.comUser = {};
				})
			}; // END $scope.fetchUser()========================
			$scope.combine = function() {
				if( ! checkCanBeCombine() ){
					alert('输入有误');
					return false;
				}
				var url = CFG.adminDir + 'user/combine';
				var params = {
					submit: 1,
					mid: $scope.mainUser.id,
					did: $scope.delUser.id,
				}
				API.fetch(url, params).then(function(rtn) {
					$scope.comUser = rtn.user;
					$scope.comUser.school = School.getSchoolNameById(rtn.user.schoolID);
					alert('合并成功');
					$scope.tips = '合并成功!';
				}).catch(function(error){
					$scope.tips = error.msg || '合并失败';
				}).finally(function(){
					$scope.canBeCombine = false;
				});
			}; // END $scope.combine()==============================
		}
	]) // END Controller combineCtrl() ================================